package com.qys.livesMall.finance.service.impl;

import com.alibaba.fastjson2.JSON;
import com.qys.livesMall.common.utils.bean.BeanProUtils;
import com.qys.livesMall.finance.dao.FinanceShopAccountDetailDao;
import com.qys.livesMall.finance.mapper.FinanceShopAccountDetailMapper;
import com.qys.livesMall.finance.model.dto.AccountDetailSearch;
import com.qys.livesMall.finance.model.dto.AccountHandle;
import com.qys.livesMall.finance.model.dto.AccountInOut;
import com.qys.livesMall.finance.model.entity.shop.FinanceShopAccount;
import com.qys.livesMall.finance.model.entity.shop.FinanceShopAccountAfter;
import com.qys.livesMall.finance.model.entity.shop.FinanceShopAccountDetail;
import com.qys.livesMall.finance.service.FinanceShopAccountDetailService;

import com.qys.livesMall.framework.view.Paging;
import com.qys.livesMall.framework.view.QysPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 店铺余额明细 服务实现类
 * </p>
 *
 * @author
 * @since 2023-06-06
 */
@Service
public class FinanceShopAccountDetailServiceImpl  implements FinanceShopAccountDetailService {

    @Autowired
    private FinanceShopAccountDetailDao shopBalanceDetailDao;

    @Autowired
    private FinanceShopAccountDetailMapper shopBalanceDetailMapper;


    @Override
    public void add(AccountHandle handle, FinanceShopAccount balance) {
        FinanceShopAccountDetail detail=new FinanceShopAccountDetail();
        detail.setUserId(balance.getUserId());
        detail.setShopId(handle.getShopId());
        detail.setBalanceId(balance.getId());
        detail.setBusinessNo(handle.getBusinessNo());
        detail.setPayMethod(handle.getPayMethod());
        detail.setType(handle.getTradeTypeEnum().getType());
        detail.setInOut(handle.getTradeTypeEnum().getInOut());
        detail.setCategory(handle.getBusinessType().getType());
        detail.setCode(handle.getBusinessType().getCode());
        detail.setAmountIncurred(handle.getAmountIncurred());
        detail.setDescription(handle.getDescription());
        FinanceShopAccountAfter after=BeanProUtils.copyProperties(balance, FinanceShopAccountAfter::new);
        detail.setPayAfter(JSON.toJSONString(after));
        detail.setExpectedAmount(handle.getAmountIncurred());
        detail.setSerialNumber(handle.getSerialNumber());
        detail.setPayDate(LocalDateTime.now());
        shopBalanceDetailDao.save(detail);

    }

    @Override
    public void addOtherLog(AccountHandle handle, FinanceShopAccount balance) {
        FinanceShopAccountDetail detail=new FinanceShopAccountDetail();
        detail.setUserId(balance.getUserId());
        detail.setShopId(handle.getShopId());
        detail.setBalanceId(balance.getId());
        detail.setBusinessNo(handle.getBusinessNo());
        detail.setPayMethod(handle.getPayMethod());
        detail.setType(handle.getTradeTypeEnum().getType());
        detail.setInOut(handle.getTradeTypeEnum().getInOut());
        detail.setCategory(handle.getBusinessType().getType());
        detail.setCode(handle.getBusinessType().getCode());
        detail.setAmountIncurred(handle.getAmountIncurred());
        detail.setDescription(handle.getDescription());
        FinanceShopAccountAfter after=BeanProUtils.copyProperties(balance, FinanceShopAccountAfter::new);
        detail.setPayAfter(JSON.toJSONString(after));
        detail.setExpectedAmount(handle.getFreezeAmount());
        detail.setPayDate(LocalDateTime.now());
        detail.setSerialNumber(handle.getSerialNumber());
        shopBalanceDetailDao.save(detail);
    }

    @Override
    public QysPage<FinanceShopAccountDetail> list(AccountDetailSearch search) {
        Paging<FinanceShopAccountDetail> page=shopBalanceDetailMapper.search(new Paging<>(search));
        return QysPage.to(page);
    }

    @Override
    public AccountInOut sum(AccountDetailSearch search) {
        return shopBalanceDetailMapper.sum(search);
    }
}
